//
//  UnlockPwViewController.m
//  PerfectProject
//
//  Created by Meng huan on 14/12/17.
//  Copyright (c) 2014年 M.H Co.,Ltd. All rights reserved.
//

#import "UnlockPwViewController.h"
#import "GesturePwView.h"

@interface UnlockPwViewController () {
    // 动画进度标记
    NSInteger progress;
}

@end

@implementation UnlockPwViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // view的背景颜色
    self.view.backgroundColor = [UIColor blackColor];
    
    // 背景图片
    UIImageView *background = [[UIImageView alloc] initWithFrame:self.view.bounds];
    background.tag          = 10;
    [self.view addSubview:background];
    
    // 切换背景图
    [self backgroundAnimation];

    
    // 手势密码
    GesturePwView *gesturePwView            = [[GesturePwView alloc] initWithFrame:self.view.bounds];
    gesturePwView.normalGestureNodeImage    = [UIImage imageWithContentsOfFile:DEF_IMAGE_PATH(@"gesture_node_normal.png")];
    gesturePwView.selectedGestureNodeImage  = [UIImage imageWithContentsOfFile:DEF_IMAGE_PATH(@"gestrue_node_selected.png")];
    gesturePwView.lineColor                 = [[UIColor greenColor] colorWithAlphaComponent:0.5];
    gesturePwView.lineWidth                 = 3;
    gesturePwView.contentInsets             = UIEdgeInsetsMake(180, 50, 180, 50);    // 4列20个
    [self.view addSubview:gesturePwView];
    
    // 防止循环引用
    __unsafe_unretained typeof(gesturePwView) pwView = gesturePwView;
    
    pwView.gesturePwBeginWithPasscode   = ^(NSString *result){
        NSLog(@"手势密码输入开始: %@", result);
    };
    pwView.gesturePwEndWithPasscode     = ^(NSString *result){
        NSLog(@"手势密码输入结束: %@", result);
        
        // 在此做密码比对
        NSString *defultPw = @"0,1,2,3";
        if ([result isEqualToString:defultPw])
        {
            // 密码相同
            [self.view removeFromSuperview];
        }
        else
        {
            // 密码错误
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil
                                                         message:@"密码错误"
                                                        delegate:nil
                                               cancelButtonTitle:@"好"
                                               otherButtonTitles:nil, nil];
            [av show];
        }
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 背景图片简单切换
- (void)backgroundAnimation
{
    NSString *imgPath       = [NSString stringWithFormat:@"gesture_background_%ld.png", (long)progress];
    UIImageView *background = (UIImageView *)[self.view viewWithTag:10];
    background.image        = [UIImage imageWithContentsOfFile:DEF_IMAGE_PATH(imgPath)];
    
    [UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionCurveLinear animations:^{
        //
        background.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        //
        progress++;
        
        if (progress > 1) {
            progress = 0;
        }
        
        NSString *imgPath       = [NSString stringWithFormat:@"gesture_background_%ld.png", (long)progress];
        background.image        = [UIImage imageWithContentsOfFile:DEF_IMAGE_PATH(imgPath)];
        
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            //
            background.alpha = 1;
            
        } completion:^(BOOL finished) {
            
            [self backgroundAnimation];
        }];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
